public class MyBlockingDeque {
    //1. 循环队列(用数组实现)
    //2. 线程安全
    //3. 阻塞队列，入队时满了之后就阻塞，出队时空了之后也阻塞

    //有效空间为10
    int[] arr = new int[10];
    //head指向入队的下标,tail指向出队的元素
    int head = 0;
    int tail = 0;

    //入队操作
    public int put(int tmp) throws InterruptedException {
        synchronized (this) {
            //如果满了就阻塞该线程
            while (isFull()) {
                this.wait();
            }
            arr[head] = tmp;
            head = (head + 1) % arr.length;

            //当入队时就可以notify
            this.notify();
        }
        return tmp;
    }

    //出队操作
    public int take() throws InterruptedException {
        synchronized (this) {
            //为null时就等待
            while (isNull()) {
                this.wait();
            }
            int oldTail = arr[tail];
            tail = (tail + 1) % arr.length;
            this.notify();
            return oldTail;
        }
    }

    //判满
    public boolean isFull() {
        if ((head + 1) % arr.length == tail) {
            return true;
        }
        return false;
    }
    //判空
    public boolean isNull() {
        if (head == tail) {
            return true;
        }
        return false;
    }


}
